package com.gps.pe.service;

import java.sql.Connection;

import org.apache.log4j.Logger;

import com.gps.pe.dao.DAOManagerFactory;
import com.gps.pe.dao.DatabaseConnection;
import com.gps.pe.exception.DAOException;
import com.gps.pe.exception.PeException;
import com.gps.pe.model.User;

public class UserService implements IUserService {

	private static final Logger LOGGER = Logger.getLogger(UserService.class);

	/**
	 * Service to get User by Id
	 */
	@Override
	public User getUserByLoginId(String loginId) throws PeException {
		User user = null;
		LOGGER.info("Service started - Get role by Id [" + loginId + "]");
		Connection connection = DatabaseConnection.getInstance().getConnection();
		DAOManagerFactory daoFactory = new DAOManagerFactory(connection);
		try {
			daoFactory.beginTransaction();
			LOGGER.info("Begin transaction...");
			user = daoFactory.getUserDAO().getUserByLoginId(loginId);
		} catch (DAOException e) {
			LOGGER.error("DAOException to get User by Id [" + loginId + "]");
			daoFactory.rollbackTransaction();
			throw new PeException("Problem to fetch data for User [" + loginId + "]", e);
		}
		daoFactory.endTransaction();
		LOGGER.info("Transaction ended successfully...");
		LOGGER.info("Service ended successfully - Get user by Id [" + loginId + "]");
		return user;
	}

}
